/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import Client.Login.Client_Login;
import Client.Online.Client_Dashboard;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JOptionPane;

/**
 *
 * @author TienNT
 */
public class Client {

    private String StatusText;
    private String mHostAddress = "localhost";
    private int mPort = 1233;
    private Socket mSocket;
    private BufferedReader mFromServer;
    private PrintWriter mToServer;
    private String mClientName = "";
    private Client_Login mClientLoginForm;
    private Client_Dashboard mClientDashboard;
    private String mStatus = ClientUtils.STATUS_NOT_CONNECTED;

    public Socket getSocket() {
        return mSocket;
    }

    public Client_Dashboard getDashboard() {
        return this.mClientDashboard;
    }

    public void setHostAddress(String hostAddress) {
        this.mHostAddress = hostAddress;
    }

    public void connectServer() {
        if (mSocket == null) {
            try {
                mSocket = new Socket(mHostAddress, mPort);
                mFromServer = new BufferedReader(new InputStreamReader(
                        mSocket.getInputStream()));
                mToServer = new PrintWriter(mSocket.getOutputStream(), true);
            } catch (IOException ex) {
            }
        }
    }

    public void setPort(int port) {
        this.mPort = port;
    }

    public String getClientName() {
        return mClientName;
    }

    public void setClientName(String name) {
        mClientName = name;
    }

    public void setStatus(String status) {
        mStatus = status;
    }

    public void sendMessageToServer(String message) {
        mToServer.println(message);
    }

    public String receiveMessage() {

            try {
                return mFromServer.readLine();
            } catch (IOException ex) {
                return null;
            }



    }

    public Client() {
    }

    public void run() {
        // TODO Auto-generated method stub
        while (true) {
            switch (mStatus) {
                case ClientUtils.STATUS_NOT_CONNECTED:
                    if (mClientLoginForm == null) {
                        
                        mClientLoginForm = new Client_Login(this);
                        mClientLoginForm.setVisible(true);
                    }
                    break;
                case ClientUtils.STATUS_CONNECTED:
                    if (mClientDashboard == null) {
                        mClientDashboard = new Client_Dashboard(this, this.StatusText);
                        mClientDashboard.setVisible(true);
                        mClientDashboard.run();
                       
                    }
                    break;
                default:
                    break;
            }
        }
    }

    public void closeSession() {
        try {
            mFromServer.close();
            mToServer.close();
            mSocket.close();
            System.exit(0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        }
        new Client().run();
    }
}
